#导入jwt类

import jwt
#导入时间库
import datetime
from settings import MIYAO
#秘钥类
class MyJwt:
    def __init__(self):
        #初始化秘钥
        self.password = MIYAO
    #加密秘钥 添加生命周期
    def encode_jwt_time(self,userinfo,mytime):
      #单独控制载荷信息   #用户信息
      playload = {'exp':int((datetime.datetime.now()+datetime.timedelta(seconds=mytime)).timestamp()),
      
       'data':userinfo
      }
      mystr = jwt.encode(playload,self.password,algorithm="HS256")
      return mystr
  
    #加密  algorithm='HS256'秘钥
    def encode_jwt(self,userinfo):
        mystr =  jwt.encode(userinfo,self.password,algorithm="HS256")
        try:
            mystr = str(mystr,'utf-8')
        except Exception as e:
            pass
        return mystr
    #解密
    def decode_jwt(self,mystr):
        return jwt.decode(mystr,self.password,algorithms=["HS256"])

# myjwt = MyJwt()
# str1 = myjwt.encode_jwt_time({'id':4,'username':'hhh'},30)
# print(myjwt.decode_jwt(str1))
# print(myjwt.encode_jwt_time({'id':4,'username':'hhh'},30))

# res = myjwt.encode_jwt({'id':1})
# print(res)
# encode_jwt =myjwt.encode_jwt({'id':1,}) 
# print(myjwt.decode_jwt(encode_jwt)['id'])